<?php
include("utils.php");
if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
else
{
	main_content("main();");
}

function main()
{

if($_SESSION['languageId']!="")
{
	$languageId=$_SESSION['languageId'];
}
else
{
	$languageId='1';
}

if($_REQUEST['mode']=='sales_history')
{
//  echo "<script>alert('hi')</script>";
$today=date("Y-m-d");


//echo $today;
$sql_details="SELECT ".SITEMASTER.".*, ".ADSPSCEMASTER.".*, ".ADVDETAILS.".*, ".ORDER_DETAILS.".advertiseId AS aid, ".ADVERTISEMASTER.".* FROM ".SITEMASTER.", ".ADSPSCEMASTER.", ".ADVDETAILS.", ".ORDER_DETAILS.", ".ADVERTISEMASTER." WHERE ".SITEMASTER.".siteuserId = '".$_SESSION['siteuserId']."' AND ".SITEMASTER.".adsiteId = ".ADSPSCEMASTER.".adsiteId AND ".ADSPSCEMASTER.".UserId = '".$_SESSION['siteuserId']."' AND ".ADSPSCEMASTER.".adspaceId = ".ADVDETAILS.".adspaceId AND ".SITEMASTER.".adsiteId = ".ADVDETAILS.".adsiteId AND ".ADVDETAILS.".advertiseId = ".ORDER_DETAILS.".advertiseId and ".ADVERTISEMASTER.".advertiseId = ".ADVDETAILS.".advertiseId and ".ADVERTISEMASTER.".startDate >= '".$_REQUEST['start_date']."' AND ".ADVERTISEMASTER.".endDate <= '".$_REQUEST['end_date']."'";

}

else
{

//  $sql_details="SELECT * FROM ".SITEMASTER." AS SM, ".ADSPSCEMASTER." AS ASM, ".ADVDETAILS." AS ADTLS WHERE SM.siteuserId = '".$_SESSION['siteuserId']."' AND SM.adsiteId = ASM.adsiteId AND ASM.UserId = '".$_SESSION['siteuserId']."' AND ASM.adspaceId = ADTLS.adspaceId AND SM.adsiteId = ADTLS.adsiteId";

$sql_details="SELECT ".SITEMASTER.".*, ".ADSPSCEMASTER.".*, ".ADVDETAILS.".*, ".ORDER_DETAILS.".advertiseId AS aid FROM ".SITEMASTER.", ".ADSPSCEMASTER.", ".ADVDETAILS.", ".ORDER_DETAILS." WHERE ".SITEMASTER.".siteuserId = '".$_SESSION['siteuserId']."' AND ".SITEMASTER.".adsiteId = ".ADSPSCEMASTER.".adsiteId AND ".ADSPSCEMASTER.".UserId = '".$_SESSION['siteuserId']."' AND ".ADSPSCEMASTER.".adspaceId = ".ADVDETAILS.".adspaceId AND ".SITEMASTER.".adsiteId = ".ADVDETAILS.".adsiteId AND ".ADVDETAILS.".advertiseId = ".ORDER_DETAILS.".advertiseId";

	
}
//echo $sql_details;
$query_details=mysql_query($sql_details);
$num=mysql_num_rows($query_details);

?>
<script language="JavaScript">
function search_sales_history()
{
document.adspaces.mode.value="sales_history";
document.adspaces.submit();
}
</script>
<!--<SCRIPT language=javascript src="calender/js/popcalendar.js"></SCRIPT>-->
<script language="javascript" type="text/javascript" src="JavaScripts/datetimepicker.js"></script>
<form name="adspaces" action="" method="POST" id="adspaces" onsubmit="return search_sales_history();">
<input type="hidden" name="mode" value="">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top"><table  border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                  <td width="244" valign="top"><?php include("left_pannel.php"); ?></td><td width="14" valign="top">&nbsp;</td>
                    <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                            <tr>
                              <td class="BoxContent">
<!-- ############# design starts ############# -->
<table id="spacesList" style="width:100%;margin-left:1px;position:relative" align="center" border="0" >

<tr>
    <td colspan="2" align="center" class="sky font-18" style="padding-bottom:20px;">
	Sales History
	</td>
<?php
if($GLOBALS['err_msg']!="")
{
?>
<tr>
    <td colspan="2" align="center" class="font-11 latest_heading2" style="padding-bottom:10px;">
	<strong><?php echo $GLOBALS['err_msg'];?></strong>
	</td>
</tr>
<?php
}
?>
<?php
$current_date=date("Y-m-d");
//echo $current_date;
$explode_current_date=explode("-",$current_date);

$explode_next_year=$explode_current_date[0]+1;
$nextyear=$explode_next_year."-".date("m")."-".date("d");

$explode_previous_year=$explode_current_date[0]-1;
$previousyear=$explode_previous_year."-".date("m")."-".date("d");
?>
  <tr>
	<td style="text-align:left;white-space:nowrap;padding-bottom:10px;" colspan="" valign="middle">


      <span class="font-12 black advertise_tableTxt">
      Show starts from:<input type="text" name="start_date" id="start_date" value="<?php echo $previousyear; ?>" class="InputBox" onClick="javascript:NewCal('start_date','yyyymmdd',false,24);" style="width:100;" maxlength="100" >&nbsp;to:&nbsp; 
	<input type="text" name="end_date" id="end_date" value="<?php echo $nextyear; ?>" class="InputBox" onClick="javascript:NewCal('end_date','yyyymmdd',false,24);" style="width:100" maxlength="100">
      </span>

<span class="font-12 black advertise_tableTxt">
	<input type="submit" name="search_publisher_statistics" id="search_publisher_statistics" value="Go" class="blank-button-big-add" />
	</span>
    </td>




  </tr>


  <tr>

    <td colspan="2">

<table border="1">
 <TR>
  <TD align="center">
   <table width="100%" class="login_txt" cellspacing="1" cellpadding="0" border="0">
    <tr style="background-color:gray">
	<th class="listHeader" align="center" nowrap="true">
        <strong>Date Sold</strong>
        </th>
        <th class="listHeader" align="center" nowrap="true">
        <strong>Run Dates</strong>
        </th>
        <th class="listHeader" align="center" nowrap="true">
         Website
        </th>
        <th class="listHeader" align="center" nowrap="true">
         AD Space
        </th>
	<th class="listHeader" align="center" nowrap="true">
        Days
        </th>
	<th class="listHeader" align="center" nowrap="true">
         Price
        </th>
	<th class="listHeader" align="center" nowrap="true">
         Total Earnings
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Daily Earnings
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Current Earnings
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Status
       </th>

	    <?php
		if($num>0)
		{

		$loop=0;
		while($rec_details=mysql_fetch_array($query_details))
		{
		$loop++;
		if($loop%2 == 0) 
		{
		$class1="normalActive2";
		$class2="normal2";
		$class3="normal2";
		$class4="gray_sitename2";
		}
		else 
		{
		$class1="normalActive";
		$class2="normal";
		$class3="normal";
		$class4="white_sitename2";
		}

$select_date_sold="select payout_date from ".ORDER_DETAILS." where advertiseId = '".$rec_details['advertiseId']."'";
$exe_date_sold=mysql_query($select_date_sold);
$row_date_sold=mysql_fetch_array($exe_date_sold);

$select_start_end_date="select * from ".ADVERTISEMASTER." where advertiseId = '".$rec_details['advertiseId']."'";
$exe_start_end_date=mysql_query($select_start_end_date);
$row_start_end_date=mysql_fetch_array($exe_start_end_date);
	    ?>

        <tr onmouseover="this.className='<?php echo $class1;?>'" onmouseout="this.className='<?php echo $class2;?>'" class="<?php echo $class3;?>">
	<td class="<?php echo $class4;?>" align="left">

		<?php echo $row_date_sold['payout_date'];?>

		</td>


        <td class="<?php echo $class4;?>" align="left">

		<?php echo $row_start_end_date['startDate'];?> - <?php echo $row_start_end_date['endDate'];?>

		</td>


        <td class="<?php echo $class4;?>" align="left">
		<?php
		echo $rec_details['adSiteUrl'];
		?>
		&nbsp;
	</td>


        <td class="<?php echo $class4;?>" align="left"><?php echo $rec_details['adSpaceName'];?>&nbsp;</td>


	 <td class="<?php echo $class4;?>" align="left">
	<?php
	$date1=date('m/d/Y',strtotime($row_start_end_date['startDate']));
	$date2=date('m/d/Y',strtotime($row_start_end_date['endDate']));
	
	echo $date=dateDiff("/", $date2, $date1);
	?>&nbsp;</td>


	<td class="<?php echo $class4;?>" align="left"><?php echo $row_start_end_date['adPrice'];?></td>

<?php
// $sql_total_price="select sum(AD_MASTER.adPrice) as total_price from ".ADVERTISEMASTER." AS AD_MASTER, ".ADVDETAILS." AS AD_DETAILS where AD_MASTER.advertiseId = AD_DETAILS.advertiseId AND AD_DETAILS.adspaceId = '".$rec_details['adspaceId']."'";
$sql_total_price="select sum(AD_MASTER.adPrice) as total_price from ".ADVERTISEMASTER." AS AD_MASTER, ".ADVDETAILS." AS AD_DETAILS, ".ORDER_DETAILS." AS ODT where AD_MASTER.advertiseId = AD_DETAILS.advertiseId AND AD_DETAILS.adspaceId = '".$rec_details['adspaceId']."' AND AD_MASTER.advertiseId = ODT.advertiseId";
//echo $sql_total_price;
$exe_total_price=mysql_query($sql_total_price);
$row_total_price=mysql_fetch_array($exe_total_price);
?>

	<td class="<?php echo $class4;?>" align="left"><?php echo $row_total_price['total_price'];?></td>

	<td class="<?php echo $class4;?>" align="left"><?php echo $row_start_end_date['dailyBudget'];?></td>

<?php
$sql_current_price="select sum(AD_MASTER.adPrice) as current_price from ".ADVERTISEMASTER." AS AD_MASTER, ".ADVDETAILS." AS AD_DETAILS, ".SITEMASTER." AS ST_MASTER, ".ORDER_DETAILS." AS ODT where ST_MASTER.siteuserId = '".$_SESSION['siteuserId']."' AND ST_MASTER.adsiteId = AD_DETAILS.adsiteId AND AD_MASTER.advertiseId = AD_DETAILS.advertiseId AND AD_MASTER.advertiseId = ODT.advertiseId";
//echo $sql_current_price;
$exe_current_price=mysql_query($sql_current_price);
$row_current_price=mysql_fetch_array($exe_current_price);
?>
		<td class="<?php echo $class4;?>" align="left"><?php echo $row_current_price['current_price'];?></td>
		
		<td class="<?php echo $class4;?>" align="left">Paid
                        </td>
        </tr>
	<?php
	}
	} // end if
	else
	{
	?>
	   <tr><TD colspan="10" align="center" class="font-12 black advertise_tableTxt">No records found</TD></tr>

	<?php
	}
	?>

       </table>

    </td>
  </tr>
</table>

   </td>
  </tr>
</table>
<!-- ##########design ends ############## -->
</td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td width="20%">&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
</form>
            
<?php
}
?>